package cn.kgc.shiroday03.util;


import cn.kgc.shiroday03.vo.PermissionVO;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/11/26
 */
public class TreeUtil {

    public static List<PermissionVO> toTree(List<PermissionVO> treeList, Integer pid) {
        List<PermissionVO> retList = new ArrayList<>();
        for (PermissionVO parent : treeList) {
            if (pid==parent.getParentId()) {
                retList.add(findChildren(parent, treeList));
            }
        }
        return retList;
    }
    private static PermissionVO findChildren(PermissionVO parent, List<PermissionVO> treeList) {
        for (PermissionVO child : treeList) {
            if (parent.getId()==child.getParentId()) {
                if (parent.getChild() == null) {
                    parent.setChild(new ArrayList<>());
                }
                parent.getChild().add(findChildren(child, treeList));
            }
        }
        return parent;
    }
}
